package com.ebt.m.wiki.view;

import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;

import com.ebt.m.data.middle.datatype.EBoolean;
import com.ebt.m.data.middle.datatype.EController;
import com.ebt.m.utils.StringUtils;
import com.ebt.m.utils.UIHelper;
import com.ebt.m.data.middle.util.InsuranceFieldUtil;
import com.ebt.m.data.middle.InsuranceObj;
import com.ebt.m.data.middle.PolicyNodeMeta;
import com.ebt.m.wiki.view.WikiTab2InputView.OnValueChangedListener;
import com.ebt.m.wiki.view.WikiTab2OptionView.OnCheckedListener;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Michael.li
 */
public class AutoViewsFactory {
    private Context mContext;

    private ViewGroup mScrollContainer;
    private WikiTab2InputView coverageView;// 保额输入
    private WikiTab2InputView premiumView;// 保费输入

    private InsuranceObj mObj;

    private Handler mCollapseHandler;

    private boolean mFromWikiView = false;

    public AutoViewsFactory(Context context, ViewGroup container) {
        super();
        this.mContext = context;
        mScrollContainer = container;
    }

    /**
     * 动态生成左侧控制区的view
     */
    List<PolicyNodeMeta> list = new ArrayList<PolicyNodeMeta>();

    public void generateViews(InsuranceObj obj) {
        mObj = obj;
        mScrollContainer.removeAllViews();
        WikiTab2OptionView optionView;
        EController c = null;
        list = obj.getControllerNode();
        for (PolicyNodeMeta meta : list) {
            c = (EController) meta.getEBTValue();
            int[] enables = c.getEnabled();
            int selectIndex = c.getSelectedIndex();
            optionView = getOptionView(mContext);
            optionView.setFieldName(meta.getFieldName());
            optionView.setOnCheckedListener(new OnCheckedListener() {
                @Override
                public void OnChecked(WikiTab2OptionView view) {
                    mObj.setControllerSelectedIndexByFieldName(view.getFieldName(), view.getSelectedIndex());
                    checkEnable();
                    refreshView();
                }
            });
            mScrollContainer.addView(optionView);
            optionView.setData(c.getDisplayName(), c.getItemDisPlay(), enables, selectIndex, WikiTab2OptionView.TYPE_DEFAULT);
        }
        // 保额计算保费
        boolean mode = ((EBoolean) obj.getNodeValue(InsuranceFieldUtil.FIELDNAME_CPCALCULATEMODE)).getValue();
        double[] initCoverage = obj.getInitCoverage();
        double[] initPreminum = obj.getInitPreminum();

        coverageView = getInputView(mContext);
        if (obj.isDisCoverage() && !obj.isCoverageHide()) {
            mScrollContainer.addView(coverageView);
        }

        coverageView.setData("保额", initCoverage[0], initCoverage[2], initCoverage[1], initCoverage[3], mode, mObj.getCoverageDIF(), mObj.getCPUnitStr()[0]);
        coverageView.setKeyboardAllowed(obj.getCKeyboardAllowed());
        coverageView.setOnValueChangedListener(new OnValueChangedListener() {

            @Override
            public void onValueChanged(double value) {
                // Log.i("EBT", "保额输入值改变了..." + value);
                if (premiumView != null) {
                    mObj.setCoverage(StringUtils.format2Double(value));
                    refreshView();
                    coverageView.setEnabled();
                }
            }

            @Override
            public void onInputInvalid() {
                UIHelper.makeToast(mContext, "保额输入值不合法", true);
            }
        });

        premiumView = getInputView(mContext);
        mScrollContainer.addView(premiumView);
        String premiumName = "保费";
        String customeName = obj.getPremiumReplaceName();
        if (!"".equalsIgnoreCase(customeName)) {
            premiumName = customeName;
        }
        premiumView.setData(premiumName, initPreminum[0], initPreminum[2], initPreminum[1], initPreminum[3], !mode, mObj.getPremiumDIF(), mObj.getCPUnitStr()[1]);
        premiumView.setKeyboardAllowed(obj.getPKeyboardAllowed());
        premiumView.setOnValueChangedListener(new OnValueChangedListener() {

            @Override
            public void onValueChanged(double value) {
                // Log.i("EBT", "保费输入值改变了..." + value);
                if (premiumView != null) {
                    mObj.setPremium(StringUtils.format2Double(value));
                    refreshView();
                    premiumView.setEnabled();
                }
            }

            @Override
            public void onInputInvalid() {
                UIHelper.makeToast(mContext, "保费输入值不合法", true);
            }
        });
        coverageView.updateValue(mObj.getCoverage());
        premiumView.updateValue(mObj.getPremium());

        if (mFromWikiView) {

            WikiTab2CollapseView collapseView = getCollapseView(mContext);
            collapseView.setAllCollapseOnClickListener((new WikiTab2CollapseView.OnAllCollapsedListener() {
                @Override
                public void OnCollapseClick() {
                    if (mCollapseHandler != null) {
                        mCollapseHandler.sendEmptyMessage(2);// MSG_HIDE_AND_TOGGLE
                    } else {
                    }
                }

                @Override
                public void OnLeftButtonClick() {
                    if (mCollapseHandler != null) {
                        mCollapseHandler.sendEmptyMessage(3);// reset
                    } else {
                    }
                }

                @Override
                public void OnRightButtonClick() {
                    if (mCollapseHandler != null) {
                        mCollapseHandler.sendEmptyMessage(4);// favorate
                    } else {
                    }
                }

            }));

            mScrollContainer.addView(collapseView);
        }
    }

    public void generateViewsNew(InsuranceObj obj) {
        mObj = obj;
        // 保额计算保费
        boolean mode = ((EBoolean) obj.getNodeValue(InsuranceFieldUtil.FIELDNAME_CPCALCULATEMODE)).getValue();
        double[] initCoverage = obj.getInitCoverage();
        double[] initPreminum = obj.getInitPreminum();

        coverageView = getInputView(mContext);
        if (obj.isDisCoverage()) {
            mScrollContainer.addView(coverageView);
        }

        coverageView.setData("保额", initCoverage[0], initCoverage[2], initCoverage[1], initCoverage[3], mode, mObj.getCoverageDIF(), mObj.getCPUnitStr()[0]);
        coverageView.setKeyboardAllowed(obj.getCKeyboardAllowed());
        coverageView.setOnValueChangedListener(new OnValueChangedListener() {

            @Override
            public void onValueChanged(double value) {
                // Log.i("EBT", "保额输入值改变了..." + value);
                if (premiumView != null) {
                    mObj.setCoverage(StringUtils.format2Double(value));
                    refreshView();
                    coverageView.setEnabled();
                }
            }

            @Override
            public void onInputInvalid() {
                UIHelper.makeToast(mContext, "保额输入值不合法", true);
            }
        });

        premiumView = getInputView(mContext);
        mScrollContainer.addView(premiumView);
        String premiumName = "保费";
        String customeName = obj.getPremiumReplaceName();
        if (!"".equalsIgnoreCase(customeName)) {
            premiumName = customeName;
        }
        premiumView.setData(premiumName, initPreminum[0], initPreminum[2], initPreminum[1], initPreminum[3], !mode, mObj.getPremiumDIF(), mObj.getCPUnitStr()[1]);
        premiumView.setKeyboardAllowed(obj.getPKeyboardAllowed());
        premiumView.setOnValueChangedListener(new OnValueChangedListener() {

            @Override
            public void onValueChanged(double value) {
                // Log.i("EBT", "保费输入值改变了..." + value);
                if (premiumView != null) {
                    mObj.setPremium(StringUtils.format2Double(value));
                    refreshView();
                    premiumView.setEnabled();
                }
            }

            @Override
            public void onInputInvalid() {
                UIHelper.makeToast(mContext, "保费输入值不合法", true);
            }
        });
        coverageView.updateValue(mObj.getCoverage());
        premiumView.updateValue(mObj.getPremium());

        if (mFromWikiView) {

            WikiTab2CollapseView collapseView = getCollapseView(mContext);
            collapseView.setAllCollapseOnClickListener((new WikiTab2CollapseView.OnAllCollapsedListener() {
                @Override
                public void OnCollapseClick() {
                    if (mCollapseHandler != null) {
                        mCollapseHandler.sendEmptyMessage(2);// MSG_HIDE_AND_TOGGLE
                    } else {
                    }
                }

                @Override
                public void OnLeftButtonClick() {
                    if (mCollapseHandler != null) {
                        mCollapseHandler.sendEmptyMessage(3);// reset
                    } else {
                    }
                }

                @Override
                public void OnRightButtonClick() {
                    if (mCollapseHandler != null) {
                        mCollapseHandler.sendEmptyMessage(4);// favorate
                    } else {
                    }
                }

            }));

            mScrollContainer.addView(collapseView);
        }
    }

    /**
     * 检查可用性
     */
    public void checkEnable() {
        EController c = null;
        list = mObj.getControllerNode();
        int child = 0;
        for (PolicyNodeMeta meta : list) {
            c = (EController) meta.getEBTValue();
            // double[] items = c.getItems();
            int[] enables = c.getEnabled();// 所有的
            int selectIndex = c.getSelectedIndex();
            // c.getAutoSelLogic() R L
            View vv = mScrollContainer.getChildAt(child);
            if (vv instanceof WikiTab2OptionView) {
                WikiTab2OptionView optionView = (WikiTab2OptionView) mScrollContainer.getChildAt(child);
                optionView.setData(c.getDisplayName(), c.getItemDisPlay(), enables, selectIndex, WikiTab2OptionView.TYPE_DEFAULT);
            }
            child++;
        }
    }

    public void refreshView() {
        coverageView.updateValue(mObj.getCoverage());
        premiumView.updateValue(mObj.getPremium());
        if (mListener != null) {
            mListener.onSettingChanged();
        }
    }

    private OnSettingChangedListener mListener;

    public void setOnSettingChangedListener(OnSettingChangedListener listener) {
        this.mListener = listener;
    }

    public interface OnSettingChangedListener {

        void onSettingChanged();
    }

    protected WikiTab2OptionView getOptionView(Context context) {
        return new WikiTab2OptionView(context);
    }

    protected WikiTab2InputView getInputView(Context context) {
        return new WikiTab2InputView(context);
    }

    protected WikiTab2CollapseView getCollapseView(Context context) {
        return new WikiTab2CollapseView(context);
    }

    public void setCollapseHandler(Handler handler) {
        this.mCollapseHandler = handler;
    }

    public void setCollapseHandlerWeak(WeakReference<Handler> wrHandler) {
        Handler handler = wrHandler.get();
        if (handler != null) {
            this.mCollapseHandler = handler;
        }

    }

    public void setFromWikiView(boolean fromWikiView) {
        mFromWikiView = fromWikiView;
    }
}
